﻿/*
 * Created by SharpDevelop.
 * User: wing
 * Date: 2018/2/15
 * Time: 17:53
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace BuriedPointsForYMT
{
	/// <summary>
	/// Description of ExcelTool.
	/// </summary>
	public class ExcelTool
	{
		
		public ExcelTool()
		{
		}
		
		public static DataSet LoadDataFromExcel(string filePath) 
		{ 
		    try 
		    { 
		    	filePath = filePath == "" ? "D:\\0202埋点文档.xlsx" : filePath;

		        string strConn; 
		        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'"; 
		        OleDbConnection OleConn = new OleDbConnection(strConn); 
		        OleConn.Open(); 
		        String sql = "SELECT * FROM  [spm$]";//可是更改Sheet名称，比如sheet2，等等  
		 
		        OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); 
		        DataSet OleDsExcle = new DataSet(); 
		        OleDaExcel.Fill(OleDsExcle, "spm"); 
		        OleConn.Close(); 
		        return OleDsExcle; 
		    } 
		    catch (Exception err) 
		    { 
		        MessageBox.Show("数据绑定Excel失败!失败原因：" + err.Message, "提示信息", 
		            MessageBoxButtons.OK, MessageBoxIcon.Information); 
		        return null; 
		    } 
		} 
	}
}
